import java.util.*;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public  TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
        }
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.left = H;
        return A;
    }
    void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    void postOrder(TreeNode root) {
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    List<Integer> ret = new ArrayList<>();
    public List<Integer> preOrderTraversal(TreeNode root) {
        if (root == null) {
            return null;
        }
        ret.add(root.val);
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
        return ret;
    }
    public int leafSize;
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }
    int getLeaveNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if (k == 1){
           return 1;
        }
        return getLeaveNodeCount(root.left,k-1)+
        getLeaveNodeCount(root.right, k-1);
    }
    boolean isCompleteTree(TreeNode root) {
        if(root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode tem = queue.poll();
            if(tem != null) {
                queue.offer(tem.left);
                queue.offer(tem.right);
            }else{
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode tem = queue.peek();
            if (tem == null){
                queue.poll();
            }else {
                return false;
            }
        }
        return true;
    }
//     void inOrder(TreeNode root) {
//        if (root == null) {
//            return ;
//        }
//        Stack<TreeNode> stack = new Stack<>();
//         TreeNode cur = root;
//        while(cur != null || !stack.isEmpty())
//        {
//            while(cur != null) {
//                stack.push(cur);
//                cur = cur.left;
//            }
//            TreeNode top = stack.pop();
//            System.out.print(top.val+" ");
//            cur = top.right;
//        }


    }
}
